package org.aynu.orderservice.common;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * 顺序常量
 *
 * @author 小粥
 * @since 2025/03/03
 */

public class OrderConstants {

    /** 状态 等待付款 */// 订单状态
    public static final String STATUS_WAIT_PAY = "pending"; // 待支付
    /** 状态处理 */
    public static final String STATUS_PROCESSING = "processing"; // 处理中
    /** 状态 已发货 */
    public static final String STATUS_SHIPPED = "shipped"; // 已发货
    /** 已交付状态 */
    public static final String STATUS_DELIVERED = "delivered"; // 已送达
    /** 状态 已完成 */
    public static final String STATUS_COMPLETED = "completed"; // 已完成
    /** 状态 已取消 */
    public static final String STATUS_CANCELLED = "cancelled"; // 已取消

    /** Cancellable 状态 */// 可取消的状态集合
    public static final Set<String> CANCELLABLE_STATUS = new HashSet<>(Arrays.asList(
            STATUS_WAIT_PAY, STATUS_PROCESSING
    ));

    /** 成功创建订单 */// 成功消息
    public static final String SUCCESS_CREATE_ORDER = "订单创建成功";
    /** 成功库存充足 */
    public static final String SUCCESS_STOCK_ENOUGH = "库存充足";
    /** 成功更新订单状态 */
    public static final String SUCCESS_UPDATE_ORDER_STATUS = "订单状态更新成功";
    /** 成功取消订单用户 */
    public static final String SUCCESS_CANCEL_ORDER_USER = "用户取消订单成功";
    /** 成功取消订单系统 */
    public static final String SUCCESS_CANCEL_ORDER_SYSTEM = "系统取消订单成功";
    /** 成功 选择订单 */
    public static final String SUCCESS_SELECT_ORDER = "查询订单成功";
    /** 成功选择订单 */
    public static final String SUCCESS_SELECT_ORDERS = "查询用户订单列表成功";
    /** 成功 无订单 */
    public static final String SUCCESS_NO_ORDERS = "用户暂无订单";

    /** 错误 invalid param */// 错误消息
    public static final String ERROR_INVALID_PARAM = "参数无效";
    /** 错误库存不足 */
    public static final String ERROR_STOCK_INSUFFICIENT = "库存不足";
    /** 错误 Stock fetch */
    public static final String ERROR_STOCK_FETCH = "库存查询失败";
    /** 错误更新股票 */
    public static final String ERROR_UPDATE_STOCK = "库存更新失败";
    /** 错误 Create Order */
    public static final String ERROR_CREATE_ORDER = "订单创建失败";
    /** 找不到错误顺序 */
    public static final String ERROR_ORDER_NOT_FOUND = "订单不存在";
    /** 错误无效状态 */
    public static final String ERROR_INVALID_STATUS = "无效的订单状态";
    /** 错误 无效过渡 */
    public static final String ERROR_INVALID_TRANSITION = "无效的状态转换";
    /** 错误 Update Order Status */
    public static final String ERROR_UPDATE_ORDER_STATUS = "订单状态更新失败";
    /** 错误禁止 */
    public static final String ERROR_FORBIDDEN = "无权限操作该订单";
    /** 错误 订单已完成 */
    public static final String ERROR_ORDER_COMPLETED = "订单已完成，无法取消";
    /** 错误订单状态 */
    public static final String ERROR_ORDER_STATUS = "订单状态不允许取消";
    /** 错误 取消订单 */
    public static final String ERROR_CANCEL_ORDER = "订单取消失败";

    /**
     * 是有效状态
     *
     * @param status 地位
     * @return boolean
     * @author 小粥
     * @since 2025/03/03
     */// 状态校验逻辑
    public static boolean isValidStatus(String status) {
        return Arrays.asList(STATUS_WAIT_PAY, STATUS_PROCESSING, STATUS_SHIPPED,
                STATUS_DELIVERED, STATUS_COMPLETED, STATUS_CANCELLED).contains(status);
    }

    /**
     * 是有效过渡
     *
     * @param currentStatus 现状
     * @param targetStatus  目标状态
     * @return boolean
     * @author 小粥
     * @since 2025/03/03
     */// 状态流转校验
    public static boolean isValidTransition(String currentStatus, String targetStatus) {
        if (currentStatus.equals(targetStatus)) {
            return false;
        }
        return switch (currentStatus) {
            case STATUS_WAIT_PAY -> targetStatus.equals(STATUS_PROCESSING) || targetStatus.equals(STATUS_CANCELLED);
            case STATUS_PROCESSING -> targetStatus.equals(STATUS_SHIPPED) || targetStatus.equals(STATUS_CANCELLED);
            case STATUS_SHIPPED -> targetStatus.equals(STATUS_DELIVERED);
            case STATUS_DELIVERED -> targetStatus.equals(STATUS_COMPLETED);
            default -> false;
        };
    }
}